﻿using GestionCompte.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GestionCompte.Helpers
{
    public static class TableHelper
    {

        public static HtmlString createTableHelper(this HtmlHelper htmlHelper, CategoryEntity categoryEntity)
        {
            /*Generate the tagbuilders used to display table*/
            TagBuilder divTag = new TagBuilder("span");
            divTag.AddCssClass("tableHelperContainer");

            TagBuilder mainTag = new TagBuilder("table");
            mainTag.MergeAttribute("style", "border: 1px solid black;");

            TagBuilder headerTag = new TagBuilder("thead");

            TagBuilder headerTrTag = new TagBuilder("tr");
            
            TagBuilder headerTdTag = new TagBuilder("td");
            headerTdTag.MergeAttribute("colspan", "2");
            headerTdTag.MergeAttribute("style", "width:300px; text-align: center;border: 1px solid black;");

            headerTdTag.InnerHtml = categoryEntity.Name;

            TagBuilder bodyTag = new TagBuilder("tbody");

            List<TagBuilder> bodyTrTag;

            bodyTrTag = new List<TagBuilder>();

            bodyTrTag.AddRange(categoryEntity.Operations.Select(x => new TagBuilder("tr")
            {
                InnerHtml = new TagBuilder("td") { InnerHtml = x.OperationDate.ToString() }.ToString(TagRenderMode.Normal) + 
                new TagBuilder("td") { InnerHtml = x.OperationAmount.ToString() }.ToString(TagRenderMode.Normal)
            }));

            /*Merge all the tags*/
            //BODY
            bodyTag.InnerHtml = bodyTrTag.Select(x=>x.ToString(TagRenderMode.Normal)).Aggregate((i, j) => i + j);

            //HEADER
            headerTrTag.InnerHtml = headerTdTag.ToString(TagRenderMode.Normal);
            headerTag.InnerHtml = headerTrTag.ToString(TagRenderMode.Normal);
           
            //MAIN
            mainTag.InnerHtml = headerTag.ToString(TagRenderMode.Normal);
            mainTag.InnerHtml += bodyTag.ToString(TagRenderMode.Normal);

            //DIV CONTAINER
            divTag.InnerHtml = mainTag.ToString(TagRenderMode.Normal);

            return new HtmlString(divTag.ToString(TagRenderMode.Normal));
        }

    }
}